package com.chuangsi.project.admin.controller.app;

import com.chuangsi.common.constant.Constants;
import com.chuangsi.common.utils.SecurityUtils;
import com.chuangsi.framework.security.LoginBody;
import com.chuangsi.framework.security.service.SysLoginService;
import com.chuangsi.framework.security.service.SysPermissionService;
import com.chuangsi.framework.web.controller.BaseController;
import com.chuangsi.framework.web.domain.AjaxResult;
import com.chuangsi.project.admin.service.WechatService;
import com.chuangsi.project.system.domain.SysMenu;
import com.chuangsi.project.system.domain.SysUser;
import com.chuangsi.project.system.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@Api(tags = "APP-登录")
@RestController
@RequestMapping("/app")
public class AppLoginController extends BaseController {
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private ISysMenuService menuService;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private WechatService wechatService;

    @ApiOperation(value = "账号密码登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(2,loginBody);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @ApiOperation(value = "微信登录")
    @PostMapping("/loginByWechat")
    public AjaxResult loginByWechat(@RequestBody @Validated LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        String token = loginService.wechatLogin(loginBody.getOpenId());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }


    @ApiOperation(value = "获取用户信息")
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    @ApiOperation(value = "获取授权信息(code获取openid)")
    @GetMapping("/getOpenId/{code}")
    public AjaxResult<String> getOpenId(@PathVariable String code){
        String openId = wechatService.getOpenId(code);
        return success("获取openId成功",openId);
    }

}
